import { Component, EventEmitter, Injector, Output, OnInit, ViewChild, Input, AfterViewInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ExecutionPointerDto, TaskServiceProxy, WorkflowServiceProxy, OutsourcingWorkflowServiceProxy, UserServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { accountModuleAnimation } from '@shared/animations/routerTransition';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';
import { setTimeout } from 'timers';

@Component({
    selector: 'OrdinaryOutsourcingAudit',
    templateUrl: './OrdinaryOutsourcingAudit.component.html',
    styleUrls: ['./OrdinaryOutsourcingAudit.component.css']
})

export class OrdinaryOutsourcingAuditComponent extends AppComponentBase implements OnInit, AfterViewInit {
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    @Input() workflowid: any;
    @Output() out = new EventEmitter();

    saving = false;
    Outsourcings;
    workflow;
    constructor(injector: Injector,
        private _taskServiceProxy: TaskServiceProxy,
        private _outSourcingworkflowServiceProxy: OutsourcingWorkflowServiceProxy,
        private _userServiceProxy: UserServiceProxy,
        private _workflowService: WorkflowServiceProxy) {
        super(injector);
    }
    ngOnInit(): void {
        console.log(this.workflowid);
        this.initForm();
    }
    ngAfterViewInit() {
        console.log('ngAfterViewInit');
    }

    initForm(): void {
        this._outSourcingworkflowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(dto => {
            this.workflow = dto.workflowDto;
            console.log(dto);
        });
    }
    getOutsourings(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        console.log('4444444');
        this.primengTableHelper.showLoadingIndicator();

        this._outSourcingworkflowServiceProxy.getOutsourcingByWorkflowId(this.workflowid,
            '',
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event))
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });

    }
    audityes(): void {
        this._outSourcingworkflowServiceProxy.outsourcingFlow_execute(this.workflowid, 'yes').subscribe(dto => {
            console.log(dto);
            this.out.emit(dto);
        });
    }

    auditno(): void {
        this._outSourcingworkflowServiceProxy.outsourcingFlow_execute(this.workflowid, 'no').subscribe(dto => {
            console.log(dto);
            this.out.emit(dto);
        });
    }

    getAuditName(item: ExecutionPointerDto) {
        return item.extensionAttributes.Prompt;
    }
}
